package com.tarena.test.elasticsearch;

import com.alibaba.fastjson2.JSON;
import com.tarena.test.elasticsearch.pojo.Student;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ESDocTestRun {
    private RestHighLevelClient client;
    @Before
    public void initClient(){
        HttpHost httpHost=new HttpHost("localhost",9200,"http");
        RestClientBuilder builder= RestClient.builder(httpHost);
        client=new RestHighLevelClient(builder);
    }
    //创建一个doc 参考一个student 有name student_no location birthday
    @Test
    public void createDoc() throws Exception{
        //1. 准备一个发起创建doc的请求对象,并指明索引名称
        IndexRequest request=new IndexRequest("students");
        //2. 在请求中添加我们想要创建的doc数据,这里我们采用json格式
        request.source("{\n" +
                "  \"name\": \"张生生\",\n" +
                "  \"student_no\": \"20210001\",\n" +
                "  \"birthday\": \"2000-01-01\",\n" +
                "  \"location\": {\n" +
                "    \"lat\": 39.9042,\n" +
                "    \"lon\": 116.4074\n" +
                "  }\n" +
                "}", XContentType.JSON);
        //3. 在创建doc的时候,可以给doc设置id,也可以不设置 es会自动给一个id
        request.id("001");
        //4. 调用客户端创建索引
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        //5. 返回值包括了创建结果
        System.out.println(index.getResult());
    }
    //获取一个文档,利用文档id
    @Test
    public void getDoc() throws Exception{
        //1. 准备一个获取doc的请求对象,并指明索引名称和文档id
        GetRequest request=new GetRequest("test02","001");
        //2. 调用客户端获取doc
        GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
        //3. 解析这个对象 这里我们获取map格式的doc数据 转化成对应类型
        Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
        sourceAsMap.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
        Student student = JSON.parseObject(JSON.toJSONString(sourceAsMap), Student.class);
        System.out.println(student);
    }
    //更新文档(部分更新)
    @Test
    public void updateDoc() throws IOException {
        //1. 指明索引和文档iD
        UpdateRequest request=new UpdateRequest("test02","001");
        //2. 更新内容
        Map<String,Object> updateFileds=new HashMap<>();
        updateFileds.put("name","张三丰");
        updateFileds.put("student_no","20210002");
        //3. 放到request中
        request.doc(updateFileds);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    }
    //删除文档
    @Test
    public void deleteDoc() throws IOException {
        //1. 指明索引和文档iD
        DeleteRequest request=new DeleteRequest("test02","001");
        //2. 调用客户端删除
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
    }
}
